<template>
  <div>
    <el-card :body-style="{padding: '10px'}">
      <el-row slot="header" type="flex" align="middle" justify="space-between">
        <div>
          <span class="my-iconfont-text1">窗帘</span>
        </div>
        <div>
          <span>响应延时：</span>
          <span :class="delayClass">{{ delayMS }}</span>
        </div>
        <status-indicator :status="indicatorClass" :pulse="true" />
      </el-row>
      <el-row type="flex" justify="center" align="middle">
        <el-col :span="5">
          <el-row type="flex" justify="center" align="middle">
            <i class="iconfont icon-curtain my-obj-icon" />
          </el-row>
        </el-col>
        <el-col :span="19">
          <el-row type="flex" justify="center" align="middle">
            <el-col :span="12">
              <el-row type="flex" justify="center" align="middle" style="margin-bottom: 10px">
                <span>当前状态：</span>
                <el-switch
                  v-model="statusValue"
                  :disabled="statusDisable"
                  @change="eventCurtainStatusChange"
                />
              </el-row>
              <el-row type="flex" justify="center" align="middle" style="margin-top: 10px">
                <span>智能控制：</span>
                <i class="iconfont icon-naozi my-iconfont-no"></i>
              </el-row>
            </el-col>
            <el-col :span="12">
              <el-row type="flex" justify="center" align="middle" style="margin-bottom: 10px">
                <span>额外配置：</span><el-tag size="mini">速度</el-tag>
              </el-row>
              <el-row type="flex" justify="center" align="middle" style="margin-top: 10px">
                <el-radio-group v-model="speedCheckList" size="mini" @change="eventCurtainSpeedChange" :disabled="speedDisable">
                  <el-radio-button label="慢" />
                  <el-radio-button label="中" />
                  <el-radio-button label="快" />
                </el-radio-group>
              </el-row>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
    </el-card>
  </div>
</template>

<script>

import { curtainSpeed, curtainChange } from "@/util/data/api-CurtainControl"
// import { store } from '@/store

export default {
  name: "CurtainControl",
  data() {
    return {
      statusValue: this.$store.state.curtainControl.openStatus,
      speedCheckList: this.$store.state.curtainControl.extraSpeed,
      speedDisable: false,
      statusDisable: false,
      delayMs: null,
      delayClass: '',
      indicatorClass: ''
    }
  },
  methods: {
    eventCurtainStatusChange(status) {
      this.statusDisable = true
      let transeData = null
      const that = this
      const lastTime = new Date().getTime()
      transeData = status
      const transeDataDict = {
        status: transeData,
        time: lastTime
      }
      this.indicatorClass = 'intermediary'
      curtainChange(transeDataDict).then(response => {
        const { data } = response
        if (!data) {
          that.indicatorClass = 'negative'
          that.$store.state.curtainControl.openStatus = !status
        } else {
          that.delayMs = Math.abs((data.time - lastTime).toFixed(2))
          that.indicatorClass = 'positive'
          that.$store.state.curtainControl.openStatus = data.status
        }
        that.statusDisable = false
      }).catch(() => {
        that.statusDisable = false
        that.indicatorClass = 'negative'
        that.$store.state.curtainControl.openStatus = !status
      })
    },
    eventCurtainSpeedChange(index) {
      let transeData = null
      const that = this
      if (index === '慢') {
        transeData = 1
      } else if (index === '中') {
        transeData = 2
      } else {
        transeData = 3
      }
      const lastTime = new Date().getTime()
      this.speedDisable = true
      const transeDataDict = {
        mode: transeData,
        time: lastTime
      }
      this.indicatorClass = 'intermediary'
      curtainSpeed(transeDataDict).then(response => {
        const { data } = response
        if (!data) {
          that.indicatorClass = 'negative'
          that.$store.state.curtainControl.extraSpeed = index
        } else {
          that.delayMs = Math.abs((data.time - lastTime).toFixed(2))
          that.$store.state.curtainControl.extraSpeed = data.mode
          that.indicatorClass = 'positive'
        }
        that.speedDisable = false
      }).catch(() => {
        that.$store.state.curtainControl.extraSpeed = index
        that.indicatorClass = 'negative'
      })
    }
  },
  computed: {
    delayMS() {
      return this.delayMs + ' ms'
    },
    heartBeat() {
      return this.$store.state.serverTime
    }
  },
  watch: {
    delayMs(val) {
      if (val <= 150) {
        this.delayClass = 'my-iconfont-greet'
      } else if (val <= 300) {
        this.delayClass = 'my-iconfont-normal'
      } else {
        this.delayClass = 'my-iconfont-bad'
      }
    },
    heartBeat() {
      this.statusValue = this.$store.state.curtainControl.openStatus
      this.speedCheckList = this.$store.state.curtainControl.extraSpeed
    }
  }
}
</script>

<style scoped>
.my-iconfont-text1 {
  font-size: 25px;
  text-align: center;
  vertical-align: middle;
  color: orangered;
}
.my-obj-icon {
  text-align: center;
  font-size: 80px;
}

.my-iconfont-no {
  font-size: 34px;
}
.my-iconfont-yes {
  font-size: 34px;
  color: #409EFF;
}

.my-iconfont-greet {
  color: #42b983;
}
.my-iconfont-normal {
  color: greenyellow;
}
.my-iconfont-bad {
  color: orangered;
}
</style>
